package com.huike.game.Activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game17;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity17 extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private Spinner sp_game17;
    private Button but_game17_select;
    private Button but_game17_set;
    private RequestQueue queue;
    private ListView lv_game17;
    private List<Game17.DataBean> list;
    private ProgressDialog show;
    private   AdapterHoler holer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game17);
        queue = Volley.newRequestQueue(this);
        list = new ArrayList<>();
        initView();
        initData();


//        批量设置
        but_game17_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                final View inflate = View.inflate(GameActivity17.this, R.layout.alert_game17, null);

                AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity17.this);
                final AlertDialog dialog = builder.create();
                dialog.setView(inflate);

                Button  mButGame17Confirm =  inflate.findViewById(R.id.but_game17_confirm);
                Button  mButGame17Close =  inflate.findViewById(R.id.but_game17_close);

                //确定
                mButGame17Confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        EditText  mEtGame17Red =  inflate.findViewById(R.id.et_game17_red);
                        EditText mEtGame17Yellow =  inflate.findViewById(R.id.et_game17_yellow);
                        EditText  mEtGame17Green = inflate.findViewById(R.id.et_game17_green);

                         String red = mEtGame17Red.getText().toString();
                         String green = mEtGame17Green.getText().toString();
                         String yellow = mEtGame17Yellow.getText().toString();

                        JSONObject object = new JSONObject();
                        try {

                            StringBuffer buffer = new StringBuffer();
                            for(int i = 0; i<list.size(); i++){
                                if(list.get(i).getClick()){
                                    buffer.append(i).append(",");
                                }
                            }

                            Log.e("---",  buffer.toString());
                            object.put("id",buffer.deleteCharAt(buffer.length() -1));
                            object.put("red",red);
                            object.put("green",green);
                            object.put("yellw",yellow);
                            Log.e("---",object.toString());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        Toast.makeText(GameActivity17.this, object.toString(), Toast.LENGTH_SHORT).show();

//                        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "",object.toString(), new Response.Listener<JSONObject>() {
//                            @Override
//                            public void onResponse(JSONObject jsonObject) {
//
//                            }
//                        }, null);
//                        queue.add(request);

                    }
                });


                //取消
                mButGame17Close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });


                dialog.show();





            }
        });

        sp_game17.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, final int i, long l) {
                but_game17_select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {


                        switch (i){

                            /**
                             * 路口接口没写
                             */

                            case 0://路口升序
                                break;
                            case 1://路口降序
                                break;
                            case 2://红灯升序

                                Collections.sort(list, new Comparator<Game17.DataBean>() {
                                    @Override
                                    public int compare(Game17.DataBean dataBean, Game17.DataBean t1) {
                                        return Integer.parseInt(dataBean.getRed()) - Integer.parseInt(t1.getRed());
                                    }
                                });
                                holer.notifyDataSetChanged();

                                break;
                            case 3://红灯降序

                                Collections.sort(list, new Comparator<Game17.DataBean>() {
                                    @Override
                                    public int compare(Game17.DataBean dataBean, Game17.DataBean t1) {
                                        return Integer.parseInt(dataBean.getRed()) - Integer.parseInt(t1.getRed());
                                    }
                                });
                                Collections.reverse(list);
                                holer.notifyDataSetChanged();


                                break;
                            case 4://绿灯升序
                                Collections.sort(list, new Comparator<Game17.DataBean>() {
                                    @Override
                                    public int compare(Game17.DataBean dataBean, Game17.DataBean t1) {
                                        return Integer.parseInt(dataBean.getGreen()) - Integer.parseInt(t1.getGreen());
                                    }
                                });
                                holer.notifyDataSetChanged();
                                Toast.makeText(GameActivity17.this, "绿灯升序", Toast.LENGTH_SHORT).show();


                                break;
                            case 5://绿灯降序
                                Collections.sort(list, new Comparator<Game17.DataBean>() {
                                    @Override
                                    public int compare(Game17.DataBean dataBean, Game17.DataBean t1) {
                                        return Integer.parseInt(dataBean.getGreen()) - Integer.parseInt(t1.getGreen());
                                    }
                                });
                                Collections.reverse(list);
                                holer.notifyDataSetChanged();

                                break;
                            case 6://黄灯升序

                                Collections.sort(list, new Comparator<Game17.DataBean>() {
                                    @Override
                                    public int compare(Game17.DataBean dataBean, Game17.DataBean t1) {
                                        return Integer.parseInt(dataBean.getYellow()) - Integer.parseInt(t1.getYellow());
                                    }
                                });
                                holer.notifyDataSetChanged();


                                break;
                            case 7://黄灯降序

                                Collections.sort(list, new Comparator<Game17.DataBean>() {
                                    @Override
                                    public int compare(Game17.DataBean dataBean, Game17.DataBean t1) {
                                        return Integer.parseInt(dataBean.getYellow()) - Integer.parseInt(t1.getYellow());
                                    }
                                });
                                Collections.reverse(list);
                                holer.notifyDataSetChanged();


                                break;

                        }


                    }
                });

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

    }

    private void  show(){
         show = ProgressDialog.show(this, "提示", "数据加载中...");
        show.show();
    }
    private void hide (){
            show.dismiss();
    }

    private void initData() {

        show();
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "redgreenlight", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                hide();

                try {
                    JSONArray data = jsonObject.getJSONArray("data");
                    Game17.DataBean bean;
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject object = data.getJSONObject(i);
                        String red = object.getString("red");
                        String green = object.getString("green");
                        String yellow = object.getString("yellow");
                        Log.e("---",red);
                        bean = new Game17.DataBean();
                        bean.setRed(red);
                        bean.setClick(false);
                        bean.setYellow(yellow);
                        bean.setGreen(green);
                        list.add(bean);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }


                 holer = new AdapterHoler();
                lv_game17.setAdapter(holer);


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(GameActivity17.this, "数据加载失败！", Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);
    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        sp_game17 = (Spinner) findViewById(R.id.sp_game17);
        but_game17_select = (Button) findViewById(R.id.but_game17_select);
        but_game17_set = (Button) findViewById(R.id.but_game17_set);
        lv_game17 = findViewById(R.id.lv_game17);


        tv_right_text.setVisibility(View.GONE);
        iv_rigth_image.setVisibility(View.GONE);

        tv_content_title.setText("红路灯管理");

        iv_left_image.setImageResource(R.drawable.tiao);
        tv_left_text.setVisibility(View.GONE);


        but_game17_select.setOnClickListener(this);
        but_game17_set.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_game17_select:

                break;
            case R.id.but_game17_set:

                break;
        }
    }


    class AdapterHoler extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(GameActivity17.this, R.layout.lv_game17, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            Game17.DataBean bean = list.get(i);
            holder.tv_game17_way.setText(i+1 +"");
            holder.tv_game17_green.setText(bean.getGreen());
            holder.tv_game17_yellow.setText(bean.getYellow());
            holder.tv_game17_red.setText(bean.getRed());
            holder.ch_game17.setChecked(bean.getClick());

            final ViewHolder finalHolder = holder;
            holder.ch_game17.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(finalHolder.ch_game17.isChecked()){
                        list.get(i).setClick(true);
                    }else{
                        list.get(i).setClick(false);

                    }


                }
            });


            holder.btn_game17.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    View inflate = View.inflate(GameActivity17.this, R.layout.alert_game17, null);

                    AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity17.this);
                    final AlertDialog dialog = builder.create();
                    dialog.setView(inflate);


                    EditText  mEtGame17Red =  inflate.findViewById(R.id.et_game17_red);
                    EditText mEtGame17Yellow =  inflate.findViewById(R.id.et_game17_yellow);
                    EditText  mEtGame17Green = inflate.findViewById(R.id.et_game17_green);
                    Button  mButGame17Confirm =  inflate.findViewById(R.id.but_game17_confirm);
                    Button  mButGame17Close =  inflate.findViewById(R.id.but_game17_close);

                    String red = mEtGame17Red.getText().toString();
                    String green = mEtGame17Green.getText().toString();
                    String yellow = mEtGame17Yellow.getText().toString();


                    //确定
                    mButGame17Confirm.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Game17.DataBean dataBean = list.get(i);

                            Toast.makeText(GameActivity17.this, dataBean.getRed(), Toast.LENGTH_SHORT).show();

                        }
                    });


                    //取消
                    mButGame17Close.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                        }
                    });


                    dialog.show();


                }
            });




            return view;
        }


    }


    class ViewHolder {
        public View rootView;
        public TextView tv_game17_way;
        public TextView tv_game17_red;
        public TextView tv_game17_yellow;
        public TextView tv_game17_green;
        public CheckBox ch_game17;
        public Button btn_game17;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_game17_way = (TextView) rootView.findViewById(R.id.tv_game17_way);
            this.tv_game17_red = (TextView) rootView.findViewById(R.id.tv_game17_red);
            this.tv_game17_yellow = (TextView) rootView.findViewById(R.id.tv_game17_yellow);
            this.tv_game17_green = (TextView) rootView.findViewById(R.id.tv_game17_green);
            this.ch_game17 = (CheckBox) rootView.findViewById(R.id.ch_game17);
            this.btn_game17 = (Button) rootView.findViewById(R.id.btn_game17);
        }

    }

}
